package cn.action;

import java.io.Writer;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.factory.annotation.Autowired;

import cn.service.ApiService;
import cn.service.ApipraService;
import cn.service.CateService;
import cn.service.LogService;
import cn.service.ProjService;
import cn.service.UserService;
import cn.util.Pagination;

public class BaseAction implements ServletRequestAware, ServletResponseAware {

	@Autowired
	protected ProjService projService;
	@Autowired
	protected CateService cateService;
	@Autowired
	protected ApiService apiService;
	@Autowired
	protected ApipraService apipraService;
	@Autowired
	protected LogService logService;
	@Autowired
	protected UserService userService;
	
	public HttpServletRequest request;
	public HttpServletResponse response;
	public Pagination pagination;
	public int pageIndex = 1;
	public int rows = 10;
	public int total = 0;
	public static ResourceBundle rbInitData = ResourceBundle
			.getBundle("initdata");

	public void outJSONList(Object result, Integer total) {
		JSONObject json = new JSONObject();
		json.put("rows",result);
		json.put("total", total);
		try {
			response.setContentType("application/json;charset=UTF-8");
			request.setCharacterEncoding("UTF-8");
			response.setCharacterEncoding("UTF-8");
			Writer out = response.getWriter();
			out.write(json.toString());
			out.flush();
			out.close();
		} catch (Exception e) {

		}
	}
	
	public void outJSONMessage(Object result) {
		try {
			response.setContentType("application/json;charset=UTF-8");
			request.setCharacterEncoding("UTF-8");
			response.setCharacterEncoding("UTF-8");
			Writer out = response.getWriter();
			out.write(result.toString());
			out.flush();
			out.close();
		} catch (Exception e) {
			
		}
	}

	public void outJSONObject(Object result) {
		JSONObject json = new JSONObject();
		json.put("result",result);
		try {
			response.setContentType("application/json;charset=UTF-8");
			request.setCharacterEncoding("UTF-8");
			response.setCharacterEncoding("UTF-8");
			Writer out = response.getWriter();
			out.write(json.toString());
			out.flush();
			out.close();
		} catch (Exception e) {

		}
	}
	
	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	@JSON(serialize = false)
	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	@JSON(serialize = false)
	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public Pagination getPagination() {
		return pagination;
	}

	public void setPagination(Pagination pagination) {
		this.pagination = pagination;
	}

	public int getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}
	
	
}
